/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yunhoo.pay.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.pay.model.CostItemAccount;


import com.yunhoo.pay.model.Expense;
import com.yunhoo.pay.util.BillNumBuilder;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ExpenseImpl {

    private static String nameSpace = "expensevoucher.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static void insertExpVouc(SqlMapClient client, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("memo", exp.getMemo());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("affixAmount", exp.getAffixAmount());
        client.insert(nameSpace + "insertExpenseVoucherEmp", map);
    }

    public static void insertExpenseVoucher(Expense exp) {
        Map map = paramMap();
        map.put("voutype", exp.getVoutype());
        map.put("numDate", exp.getNumDate());
        map.put("number", exp.getNumber());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("person", exp.getUser().getCpsn_number());
        map.put("deptcode", exp.getDept().getCode());
        map.put("memo", exp.getMemo());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("affixAmount", exp.getAffixAmount());
        IbatisTemplate.insert(nameSpace + "insertExpenseVoucherEmp", map);
    }

    public static String getAccountMaxNumber(SqlMapClient client) throws SQLException {
        String ExpenseMaxNumber = (String) client.queryForObject("expensevoucher.maxExpenseAccountNumber", LoginParam.getOrg().getCode());
        if (ExpenseMaxNumber == null || ExpenseMaxNumber.length() == 0) {
            ExpenseMaxNumber = "000001";
            return ExpenseMaxNumber;
        }
        ExpenseMaxNumber = String.format("%06d%n", Integer.parseInt(ExpenseMaxNumber.trim()) + 1);
        return ExpenseMaxNumber.trim();
    }

    public static void insert(Expense exp, List<CostItemAccount> expacc) {

        System.out.println("insert in 事务");
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            Double tot = 0d;

            String number = BillNumBuilder.buildNum(client);
            exp.setNumber(number);

            for (CostItemAccount r : expacc) {
                r.setNumber(exp.getNumber());
                tot += r.getExpenseaccount();
                insertCIA(client, r);
            }

            exp.setTotal(tot);
            exp.setRemain(exp.getTotal() - exp.getRepayment());
            insertExpVouc(client, exp);

            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(ExpenseImpl.class.getName()).log(Level.SEVERE, null, ex);
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(ExpenseImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public static void update(Expense exp, List<CostItemAccount> expacc, CostItemAccount cia) {

        System.out.println("update int 事务");
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();

            Double tot = 0d;
            client.delete(nameSpace + "deleExpenseAccount", cia);
            for (CostItemAccount r : expacc) {
                r.setNumber(exp.getNumber());
                tot += r.getExpenseaccount();
                insertCIA(client, r);
            }
            exp.setTotal(tot);
            exp.setNumberStatus("1");
            updateExpenseVoucher(client, exp);

            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(ExpenseImpl.class.getName()).log(Level.SEVERE, null, ex);
            try {
                client.endTransaction();
            } catch (SQLException ex1) {
                Logger.getLogger(ExpenseImpl.class.getName()).log(Level.SEVERE, null, ex1);
            }
        }
    }

    public static void insertCIA(SqlMapClient client, CostItemAccount c) throws SQLException {
        Map map = paramMap();
        map.put("lineid", c.getLineid());
        map.put("number", c.getNumber());
        map.put("costcode", c.getCostitems().getCode());
        map.put("expenseaccount", c.getExpenseaccount());
        map.put("memo", Null.convert(c.getMemo()));
        client.insert(nameSpace + "insertCostItemAccount", map);
    }

    public static void insertCostItemAccount(CostItemAccount c) {
        Map map = paramMap();
        map.put("lineid", c.getLineid());
        map.put("number", c.getNumber());
        map.put("costcode", c.getCostitems().getCode());
        map.put("expenseaccount", c.getExpenseaccount());
        map.put("memo", Null.convert(c.getMemo()));
        IbatisTemplate.insert(nameSpace + "insertCostItemAccount", map);
    }

    public static void updateExpenseVoucher(SqlMapClient client, Expense exp) throws SQLException {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("affixAmount", exp.getAffixAmount());
        map.put("voutype", exp.getVoutype());
        client.update(nameSpace + "updateExpenseVoucher", map);
    }

    public static void updateExpenseVoucher(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("repayment", exp.getRepayment());
        map.put("remain", exp.getRemain());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payStyle", exp.getPayStyle().getCode().trim());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number().trim());
        map.put("affixAmount", exp.getAffixAmount());
        map.put("voutype", exp.getVoutype());
        IbatisTemplate.update(nameSpace + "updateExpenseVoucher", map);
    }

    public static List expensaccount(String number) {
        Map map = paramMap();
        map.put("iyear", LoginParam.getIyear());
        map.put("number", number);
        return IbatisTemplate.queryForList(nameSpace + "queryCostItemAccount", map);
    }

    public static void main(String args[]) {
    }
}
